home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / os2 / adaptor.zip / ADAPT.ZIP / adaptor / src / xfiles.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  9KB  |  318 lines

  1. /**************************************************************************
  2. *                                                                         *
  3. *  Author      : Dr. Thomas Brandes, GMD, I1.HR                           *
  4. *  Date        : Nov 92                                                   *
  5. *  Last Update : April 92                                                 *
  6. *                                                                         *
  7. *  Module      : xfiles                                                   *
  8. *                                                                         *
  9. *  Function    : Athena Widgets for File Handling                         *
  10. *                                                                         *
  11. *  Export :                                                               *
  12. *                                                                         *
  13. *    void make_file_menu_entries (Widget father)                          *
  14. *                                                                         *
  15. *  Update :    Jan 93, Werner Meurer, GMD, I1.HR                          *
  16. *                                                                         *
  17. *    - only source  files / directories should appear in the menu         *
  18. *                                                                         *
  19. **************************************************************************/
  20.  
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23.  
  24. #ifdef SYS_V
  25. #include <dirent.h>
  26. #else
  27. #include <sys/dir.h>
  28. #endif
  29.  
  30. #ifdef alliant
  31. #include <sys/param.h>    /* getwd */
  32. #else
  33. #include <unistd.h>       /* getcwd */
  34. #endif
  35.  
  36. #include <string.h>
  37.  
  38. #include "xglobal.h"
  39. #include "xfiles.h"
  40.  
  41. #define ROW    5
  42.  
  43. bool is_source_file ();
  44.  
  45. /*********************************************************************
  46. *                                                                    *
  47. *  Global Data is Array of Entries                                   *
  48. *                                                                    *
  49. *********************************************************************/
  50.  
  51. int count;                /* Number of entries */
  52.  
  53. int*  ListDirNamePtr;      /* pointer to starting places of file names */
  54. char* ListDirNames;        /* pointer to file names */
  55. int ListDirNamePtrLen = 0;
  56. int ListDirNamesLen = 0;
  57.  
  58. /* ListDirNames [ListDirNamePtr[i]] is start of string for entry i */
  59.  
  60. /* Sorting the entries */
  61.  
  62. void ListDirSort (nameptr,names,count)
  63. int *nameptr;
  64. char *names;
  65. int  count;
  66.  
  67. { short int l, r;
  68.   short int j, jfinal, k;
  69.   short int np;
  70.  
  71.     if( count <= 1 ) return;
  72.  
  73.     l = count/2;
  74.     r = count - 1;
  75.     while( r >= 1 ){
  76.         if( l > 0 ){/* buildup phase, decrement l */
  77.             --l;
  78.             j = l;
  79.         }
  80.         else{
  81.             /* selection phase, exchange [0] and [r], decrement r */
  82.             np = nameptr[0];
  83.             nameptr[0] = nameptr[r];
  84.             nameptr[r] = np;
  85.             --r;
  86.             j = 0;
  87.         }
  88.         np = nameptr[j];
  89.         jfinal = -1;
  90.  
  91.         while( (k=2*j+1) <= r && jfinal < 0 ){
  92.             /* sink [j] by interchanging it with the larger of its children */
  93.             /* find the larger child, if there are two */
  94.             if( k < r && strcmp( &(names[ nameptr[k  ] ]),
  95.                                  &(names[ nameptr[k+1] ])) < 0 ){
  96.                 k++;
  97.             }
  98.             if( strcmp( &(names[ np ]), &(names[ nameptr[ k ] ])) < 0 ){
  99.                 /* exchange [j] and [k], move on with [k] */
  100.                 nameptr[j] = nameptr[k];
  101.                 j = k;
  102.             }
  103.             else{       /* stop here */
  104.                 jfinal = j;
  105.             };
  106.         }
  107.  
  108.         /* if stopped, jfinal==j, else jfinal==0. Use j */
  109.         /* store this name into [j] */
  110.  
  111.         nameptr[j] = np;
  112.  
  113.     }
  114. }/* ListDirSort */
  115.  
  116. void ListDirMode (nameptr, names, count )
  117. int *nameptr;
  118. char *names;
  119. int count;
  120.  
  121. {
  122. int curcount;
  123. struct stat buf;
  124. int i;
  125.  
  126.     /* get the mode of each name.
  127.     /* if Directory, add / to name. If executable, add * */
  128.     for( curcount = 0; curcount < count; curcount++ )
  129.     {
  130.         i = nameptr[curcount];
  131.         stat( &(names[i]), &(buf) );
  132.         if( (buf.st_mode & S_IFMT) == S_IFDIR )
  133.            { for( ; names[i]!='\0'; i++){;}
  134.              names[i] = '/';
  135.              names[i+1] = '\0';
  136.            }
  137.         else if( buf.st_mode & 0111 != 0 )
  138.            { for( ; names[i]!='\0'; i++){;}
  139.              names[i] = '*';
  140.              names[i+1] = '\0';
  141.            }
  142.     } /* for */
  143. }/* ListDirMode */
  144.  
  145. void get_dir_entries ()
  146.  
  147. /* collect information of the current Directory */
  148.  
  149. { DIR* directory;          /* directory record       */
  150. #ifdef SYS_V
  151.   struct dirent* direntry;
  152. #else
  153.   struct direct* direntry; /* directory entry record */
  154. #endif
  155.   struct stat mode;
  156.   int  maxlen, totallen, thislen;
  157.   char *thisptr;
  158.   int curptr, curcount;
  159.  
  160.   /* open a directory, print out the names */
  161.  
  162.   directory = opendir (".");
  163.   if (directory == NULL)
  164.     { printf ("directory cannot be opened\n");
  165.       return;
  166.     }
  167.  
  168.   count = 0;
  169.   maxlen = 0;
  170.   totallen = 0;
  171.  
  172.   /* count the the number of files, get maximum length, total length */
  173.  
  174. #ifdef alliant
  175.   getwd (current_dir);
  176. #else
  177.   getcwd (current_dir, sizeof(current_dir));
  178. #endif
  179.  
  180.   sprintf (last_message, "Current Directory = %s \n", current_dir);
  181.  
  182.   for( direntry = readdir( directory ); direntry != NULL;
  183.              direntry = readdir( directory ))
  184.      { 
  185.     stat(direntry->d_name,&mode);
  186.     if ((direntry->d_name[0] != '.' ) && 
  187.        (is_source_file (direntry->d_name,strlen(direntry->d_name)) || 
  188.         ((mode.st_mode & S_IFMT) == S_IFDIR))){
  189. #ifdef SYS_V
  190.           thislen = strlen(direntry->d_name) + 1;
  191. #else
  192.           thislen = direntry->d_namlen;
  193. #endif
  194.           thisptr = direntry->d_name;
  195.           count += 1;
  196.           totallen += thislen;
  197.           maxlen = ( thislen > maxlen ) ? thislen : maxlen;
  198.      }
  199.   }
  200.  
  201.   /* allocate space to hold the names for the sort.
  202.   /* allocate space to hold the name pointers */
  203.  
  204.   if( sizeof(int)*(count+1) > ListDirNamePtrLen )
  205.     { ListDirNamePtrLen = sizeof(int)*(count+1);
  206.       ListDirNamePtr = (int*)malloc((unsigned)ListDirNamePtrLen);
  207.     }
  208.  
  209.   if( totallen + count > ListDirNamesLen )
  210.     { ListDirNamesLen = totallen + count + count;
  211.       ListDirNames = (char*)malloc((unsigned)ListDirNamesLen);
  212.     }
  213.  
  214.   rewinddir (directory);
  215.  
  216.   curptr = 0;
  217.   curcount = 0;
  218.  
  219.     for( direntry = readdir( directory ); direntry != NULL;
  220.                direntry = readdir( directory ))
  221.       {    stat(direntry->d_name,&mode);
  222.     if ((direntry->d_name[0] != '.' ) && 
  223.        (is_source_file (direntry->d_name,strlen(direntry->d_name)) || 
  224.         ((mode.st_mode & S_IFMT) == S_IFDIR))){
  225.             thisptr = direntry->d_name;
  226. #ifdef SYS_V
  227.             thislen = strlen(thisptr);
  228. #else
  229.             thislen = direntry->d_namlen;
  230. #endif
  231.             ListDirNamePtr[curcount] = curptr;
  232.             curcount += 1;
  233.             strcpy( &(ListDirNames[ curptr ]), thisptr );
  234.             curptr += thislen + 2;
  235.             ListDirNames[curptr-1] = '\0';
  236.             ListDirNames[curptr-2] = '\0';
  237.          }
  238.       }
  239.  
  240.   ListDirNamePtr[curcount] = 0;       /* zero-terminated array */
  241.  
  242.   closedir (directory);
  243.  
  244.   ListDirSort( ListDirNamePtr, ListDirNames, count );
  245.  
  246.   ListDirMode( ListDirNamePtr, ListDirNames, count );
  247.  
  248. } /* end of get_dir_entries */
  249.  
  250. /* this subroutine has only been used for tests */
  251.     
  252. void print_dir_entries ()
  253. { int i;
  254.   for (i=0; i<count; i++)
  255.     printf ("Entry = %s\n", &ListDirNames[ListDirNamePtr[i]]);
  256. }
  257.  
  258. static void DirSelect (w, client_data, garbage)
  259. Widget w;
  260. XtPointer client_data;
  261. XtPointer garbage;  /* call_data */
  262. {   Widget menu, father;
  263.     char *name;
  264.     int i;
  265.    
  266.     int pane_num = (int) client_data;
  267.     name = XtName (w);
  268.  
  269.     /* printf("Menu item %s has been selected.\n", name);
  270.        printf("Data is %d\n", pane_num);
  271.     */
  272.  
  273.     for (i=0;name[i]!='\0';i++) {;}
  274.  
  275.     if ( (pane_num == -1) || (name[i-1] == '/') )
  276.       { /* direcory name has been selected */
  277.         chdir (name);
  278.         menu = XtParent (w);
  279.         father = XtParent (menu);
  280.         XtDestroyWidget (menu);
  281.         make_file_menu_entries (father);
  282.         set_message ();   /* display current directory */
  283.       }
  284.      else
  285.       { /* file name has been selected */
  286.         strcpy (selected_file, current_dir);
  287.         strcat (selected_file, "/");
  288.         strcat (selected_file, name);
  289.         set_filelabel ();  /* Display String in labelWidget */
  290.         strcpy (last_message, "file selected");
  291.         set_message ();
  292.       }
  293. }
  294.  
  295. void make_file_menu_entries (father)
  296.  
  297. Widget father;
  298.  
  299. { int i;
  300.   Widget entry, menu;
  301.   char *item;
  302.  
  303.   get_dir_entries ();
  304.  
  305.   menu = XtCreatePopupShell("menu", simpleMenuWidgetClass,
  306.                             father, NULL, 0);
  307.  
  308.   entry = XtCreateManagedWidget ("..", smeBSBObjectClass, menu, NULL, 0);
  309.   XtAddCallback (entry, XtNcallback, DirSelect, (XtPointer) -1);
  310.  
  311.   for (i=0; i < count; i++)
  312.     { item = &ListDirNames[ListDirNamePtr[i]];
  313.       entry = XtCreateManagedWidget (item, smeBSBObjectClass, menu, NULL, 0);
  314.       XtAddCallback (entry, XtNcallback, DirSelect, (XtPointer) i);
  315.     }
  316.  
  317. }
  318.